/**
 * 自定义菜单相关接口
 * @authors 杨兴洲（of2502）
 * @date    2017/2/27 19:57
 * @version 1.0
 */
import * as rp from "request-promise-native";
const HOST = 'https://api.weixin.qq.com/cgi-bin';

export default class WeixinMenuService {

    /**
     * 查询菜单
     * @param access_token
     * @returns {Promise<any>}
     */
    static getInfo(access_token): Promise<any> {
        return rp({
            method: 'GET',
            uri: `${HOST}/menu/get`,
            qs: {access_token},
            json: true
        })
        // .then((data: any) => data.errcode ? Promise.reject(data) : data);
            .then((data: any) => data);
    }

    /**
     * 查询当前菜单
     * @param access_token
     * @returns {Promise<any>}
     */
    static getCurrentInfo(access_token: string): Promise<any> {
        return rp({
            method: 'GET',
            uri: `${HOST}/get_current_selfmenu_info`,
            qs: {access_token},
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /**
     * 创建菜单
     * @param access_token
     * @param menu
     * @returns {Promise<any>}
     */
    static create({access_token, menu}): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/menu/create`,
            qs: {access_token},
            body: {...menu},
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /**
     * 删除菜单
     * @param access_token
     * @returns {Promise<any>}
     */
    static remove(access_token): Promise<any> {
        return rp({
            method: 'GET',
            uri: `${HOST}/menu/delete`,
            qs: {access_token},
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

}
